package net.area80.composer.display
{
	import flash.display.DisplayObject;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import net.area80.composer.core.Composer;
	
	import org.osflash.signals.Signal;

	public class InOutClipComposer extends Composer
	{
		protected var o:MovieClip;
		
		public var SIGNAL_FIRSTFRAME:Signal = new Signal();
		public var SIGNAL_MOVEIN:Signal = new Signal();
		public var SIGNAL_MOVEOUT:Signal = new Signal();
		public var SIGNAL_STANDBY:Signal = new Signal();
		public var SIGNAL_LASTFRAME:Signal = new Signal();
		public var stopFirstFrame:Boolean = true;
		public var removeOnOut:Boolean = true;
		
		public function InOutClipComposer(o:DisplayObject, stopFirstFrame:Boolean=true, removeOnOut:Boolean = false)
		{
			this.stopFirstFrame = stopFirstFrame;
			this.o = o as MovieClip;
			this.removeOnOut = removeOnOut;
			init();
			super(o);
		}
		protected function init ():void {
			var labels:Array = o.currentLabels;
			var standByFrame:uint = 1;
			for(var i:uint =0;i<labels.length;i++) {
				var label:FrameLabel = labels[i] as FrameLabel;
				if(label.name=="standby") standByFrame = label.frame;
			}
			o.addFrameScript(0,onFirstFrame);
			o.addFrameScript(standByFrame-1,onStandBy);
			o.addFrameScript(o.totalFrames-1,onLastFrame);
			if(stopFirstFrame) {
				o.gotoAndStop(1);
			} else {
				o.gotoAndPlay(1);
			}
		}
		public function moveIn ():void {
			o.gotoAndPlay("in");
			SIGNAL_MOVEIN.dispatch();
		}
		public function moveOut ():void {
			o.gotoAndPlay("out");
			SIGNAL_MOVEOUT.dispatch();
		}
		protected function onFirstFrame ():void {
			if(stopFirstFrame) o.stop();
			SIGNAL_FIRSTFRAME.dispatch();
		} 
		protected function onStandBy ():void {
			o.stop();
			SIGNAL_STANDBY.dispatch();
		}
		protected function onLastFrame ():void {
			
			if(removeOnOut && o.parent){
				o.stop();
				o.parent.removeChild(o);
			}  
			SIGNAL_LASTFRAME.dispatch();
			
		}
	}
}